package com.ruoyi.zhgd.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.zhgd.mapper.NLaboratoryMechanicsWFyDetailMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.zhgd.domain.bo.NLaboratoryMechanicsWDetailBo;
import com.ruoyi.zhgd.domain.vo.NLaboratoryMechanicsWDetailVo;
import com.ruoyi.zhgd.domain.NLaboratoryMechanicsWDetail;
import com.ruoyi.zhgd.mapper.NLaboratoryMechanicsWDetailMapper;
import com.ruoyi.zhgd.service.INLaboratoryMechanicsWDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 万能机详情表信息(1,2,4标)Service业务层处理
 *
 * @author tll
 * @date 2023-09-15
 */
@RequiredArgsConstructor
@Service
@DS("sjcj")
public class NLaboratoryMechanicsWDetailServiceImpl implements INLaboratoryMechanicsWDetailService {

    private final NLaboratoryMechanicsWDetailMapper baseMapper;

    private final NLaboratoryMechanicsWFyDetailMapper nLaboratoryMechanicsWFyDetailMapper;

    /**
     * 查询万能机详情表信息(1,2,4标)
     */
    @Override
    public NLaboratoryMechanicsWDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询万能机详情表信息(1,2,4标)列表
     */
    @Override
    public TableDataInfo<NLaboratoryMechanicsWDetailVo> queryPageList(NLaboratoryMechanicsWDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<NLaboratoryMechanicsWDetail> lqw = buildQueryWrapper(bo);
        Page<NLaboratoryMechanicsWDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询万能机详情表信息(1,2,4标)列表
     */
    @Override
    public List<NLaboratoryMechanicsWDetailVo> queryList(NLaboratoryMechanicsWDetailBo bo) {
        LambdaQueryWrapper<NLaboratoryMechanicsWDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<NLaboratoryMechanicsWDetail> buildQueryWrapper(NLaboratoryMechanicsWDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<NLaboratoryMechanicsWDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getGroupId() != null, NLaboratoryMechanicsWDetail::getGroupId, bo.getGroupId());
        lqw.eq(bo.getXH() != null, NLaboratoryMechanicsWDetail::getXH, bo.getXH());
        lqw.eq(bo.getGCZJ() != null, NLaboratoryMechanicsWDetail::getGCZJ, bo.getGCZJ());
        lqw.eq(bo.getArea() != null, NLaboratoryMechanicsWDetail::getArea, bo.getArea());
        lqw.eq(bo.getLo() != null, NLaboratoryMechanicsWDetail::getLo, bo.getLo());
        lqw.eq(bo.getLe() != null, NLaboratoryMechanicsWDetail::getLe, bo.getLe());
        lqw.eq(bo.getFm() != null, NLaboratoryMechanicsWDetail::getFm, bo.getFm());
        lqw.eq(bo.getRm() != null, NLaboratoryMechanicsWDetail::getRm, bo.getRm());
        lqw.eq(bo.getFeH() != null, NLaboratoryMechanicsWDetail::getFeH, bo.getFeH());
        lqw.eq(bo.getReH() != null, NLaboratoryMechanicsWDetail::getReH, bo.getReH());
        lqw.eq(bo.getFeL() != null, NLaboratoryMechanicsWDetail::getFeL, bo.getFeL());
        lqw.eq(bo.getReL() != null, NLaboratoryMechanicsWDetail::getReL, bo.getReL());
        lqw.eq(bo.getFp() != null, NLaboratoryMechanicsWDetail::getFp, bo.getFp());
        lqw.eq(bo.getRp() != null, NLaboratoryMechanicsWDetail::getRp, bo.getRp());
        lqw.eq(bo.getLu() != null, NLaboratoryMechanicsWDetail::getLu, bo.getLu());
        lqw.eq(bo.getA() != null, NLaboratoryMechanicsWDetail::getA, bo.getA());
        lqw.eq(bo.getAt() != null, NLaboratoryMechanicsWDetail::getAt, bo.getAt());
        lqw.eq(bo.getAgt() != null, NLaboratoryMechanicsWDetail::getAgt, bo.getAgt());
        lqw.eq(StringUtils.isNotBlank(bo.getDLWZ()), NLaboratoryMechanicsWDetail::getDLWZ, bo.getDLWZ());
        lqw.eq(StringUtils.isNotBlank(bo.getDLXZ()), NLaboratoryMechanicsWDetail::getDLXZ, bo.getDLXZ());
        lqw.eq(bo.getTestBegin() != null, NLaboratoryMechanicsWDetail::getTestBegin, bo.getTestBegin());
        lqw.eq(bo.getTestEnd() != null, NLaboratoryMechanicsWDetail::getTestEnd, bo.getTestEnd());
        lqw.eq(StringUtils.isNotBlank(bo.getYSGCSJ()), NLaboratoryMechanicsWDetail::getYSGCSJ, bo.getYSGCSJ());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), NLaboratoryMechanicsWDetail::getType, bo.getType());
        return lqw;
    }

    /**
     * 新增万能机详情表信息(1,2,4标)
     */
    @Override
    public Boolean insertByBo(NLaboratoryMechanicsWDetailBo bo) {
        NLaboratoryMechanicsWDetail add = BeanUtil.toBean(bo, NLaboratoryMechanicsWDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改万能机详情表信息(1,2,4标)
     */
    @Override
    public Boolean updateByBo(NLaboratoryMechanicsWDetailBo bo) {
        NLaboratoryMechanicsWDetail update = BeanUtil.toBean(bo, NLaboratoryMechanicsWDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(NLaboratoryMechanicsWDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除万能机详情表信息(1,2,4标)
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    // 三标项目部id
    private final String deptId3 = "001001005";
    // 三标项目部code
    private final String deptCode3 = "JD04";
    // 124项目部id
    private final String[] deptId124 = new String[]{"001001001","001001003","001001009"};
    // 124项目部code
    private final String[] deptCode124 = new String[]{"JD01","JD02","JD08"};

    /**
     * 万能机设备详情表信息(1,2,4标)分页
     *
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public TableDataInfo<NLaboratoryMechanicsWDetailVo> page(NLaboratoryMechanicsWDetailBo bo, PageQuery pageQuery) {
        if (ObjectUtils.isEmpty(bo.getId())) {
            throw new ServiceException("主表id不能为空!");
        }
        // 先获取部门信息 判断项目部 124标 3标 分别调用不同明细表查询
        String code = baseMapper.getDeptById(bo.getId());
        if (StringUtils.isNotBlank(code)) {
            bo.setDeptCode(code);
            Page<NLaboratoryMechanicsWDetailVo> result = baseMapper.page(pageQuery.build(), bo);
            return TableDataInfo.build(result);
        } else {
            return null;
//            bo.setDeptCode(code);
//            Page<NLaboratoryMechanicsWDetailVo> result = nLaboratoryMechanicsWFyDetailMapper.page(pageQuery.build(), bo);
//            return TableDataInfo.build(null);
        }
//        if (StringUtils.isNotBlank(code) && ArrayUtils.contains(deptCode124, code)) {
//            bo.setDeptCode(code);
//            Page<NLaboratoryMechanicsWDetailVo> result = baseMapper.page(pageQuery.build(), bo);
//            return TableDataInfo.build(result);
//        } else {
//            bo.setDeptCode(code);
//            Page<NLaboratoryMechanicsWDetailVo> result = nLaboratoryMechanicsWFyDetailMapper.page(pageQuery.build(), bo);
//            return TableDataInfo.build(result);
//        }
    }

    @Override
    public List<NLaboratoryMechanicsWDetail> selectWDetail(Integer id) {
        return baseMapper.selectWDetail(id);
    }
}
